package com.vimi8.ebb.api.category;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.dto.CategoryDto;
import com.vimi8.ebb.dto.EnumCategoryDto;
import com.vimi8.ebb.model.EnumCategory;
import com.vimi8.ebb.service.EnumCategoryService;
import com.vimi8.ebb.util.SubStringCategoryCodeUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-09-19T10:01:46.405Z")

@Controller
public class CategoryApiController implements CategoryApi {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger("CategoryApiController");

    @Autowired
    private EnumCategoryService enumCategoryService;


    public HttpResponseEntity<String> categoryDelete(@ApiParam(value = "删除枚举分类信息.", required = true) @RequestParam(value = "id", required = true) String id


    ) {
        enumCategoryService.deleteEnumCategory(id);
        return new HttpResponseEntity<String>(HttpStatus.OK);
    }

    public HttpResponseEntity<String> categoryPut(

            @ApiParam(value = "更新分类信息", required = true) @RequestBody EnumCategory enumCategory

    ) {
        enumCategoryService.updateEnumCategory(enumCategory);
        return new HttpResponseEntity<String>(HttpStatus.OK);
    }

    public HttpResponseEntity<List<EnumCategory>> responseEnumCategoryByCateIdUsingGET(@ApiParam(value = "分类id", required = true) @RequestParam(value = "id", required = true) String id


            ,
                                                                                       @ApiParam(value = "分类级别", required = true) @RequestParam(value = "level", required = true) String level


    ) {
        int levelId = 0;
        List<EnumCategory> enumCategories = null;
        try {

            if (level != null && !"".equals(level)) {
                levelId = Integer.parseInt(level);
            }
            //根据级别转换相应的分类id
            String addressCode = SubStringCategoryCodeUtil.parseSubCategoryCode(id, levelId);
            enumCategories = enumCategoryService.getEnumCategoryListtById(addressCode, id);
        } catch (Exception ex) {
            //...
        }
        return new HttpResponseEntity<List<EnumCategory>>(enumCategories, HttpStatus.OK);
    }

    public HttpResponseEntity<String> userCategoryPostUsingPOST(

            @ApiParam(value = "添加枚举分类信息", required = true) @RequestBody EnumCategory enumCategory

    ) {
        enumCategoryService.insertEnumCategory(enumCategory);
        return new HttpResponseEntity<String>(HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<CategoryDto>> responseEnumCategoryUsingGET(@ApiParam(value = "分类id", required = true) @RequestParam(value = "id", required = true) String id, @ApiParam(value = "分类级别", required = true) @RequestParam(value = "level", required = true) String level) {
        int levelId = 0;
        List<CategoryDto> enumCategories = null;
        try {

            if (level != null && !"".equals(level)) {
                levelId = Integer.parseInt(level);
            }
            //根据级别转换相应的分类id
            String addressCode = SubStringCategoryCodeUtil.parseSubCategoryCode(id, levelId);
            enumCategories = enumCategoryService.getEnumCategoryListById(addressCode, id);
            for (CategoryDto categoryDto : enumCategories) {
                String categoryCode = SubStringCategoryCodeUtil.parseSubCategoryCode(categoryDto.getId(), categoryDto.getCategoryLevel() + 1);
                List<EnumCategory> categoryDtos = enumCategoryService.getEnumCategoryListtById(categoryCode, categoryDto.getId());
                categoryDto.setNextCategory(categoryDtos);
            }
        } catch (Exception ex) {
            //...
        }
        return new HttpResponseEntity<List<CategoryDto>>(enumCategories, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<EnumCategory>> farmCategoryGET() {
        List<EnumCategory> enumCategories = enumCategoryService.getFarmCategory();
        return new HttpResponseEntity<List<EnumCategory>>(enumCategories, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<EnumCategoryDto>> enumCategoryDtoGET(@ApiParam(value = "分类id", required = true) @RequestParam(value = "id", required = true) String id,
                                                                        @ApiParam(value = "分类级别", required = true) @RequestParam(value = "level", required = true) String level,
                                                                        @ApiParam(value = "查询分类（0表示农场，1表示产品）", required = true) @RequestParam(value = "classification", required = true) int classification) {
        int levelId = 0;
        List<EnumCategoryDto> enumCategories = null;
        try {

            if (level != null && !"".equals(level)) {
                levelId = Integer.parseInt(level);
            }
            //根据级别转换相应的分类id
            String enumCode = SubStringCategoryCodeUtil.parseSubCategoryCode(id, levelId);
            enumCategories = enumCategoryService.getEnumCategoryList(enumCode, id);
            if (classification == 0) {
                for (EnumCategoryDto enumCategoryDto : enumCategories) {
                    String enumId = SubStringCategoryCodeUtil.parseCategoryCode(enumCategoryDto.getId(), enumCategoryDto.getCategoryLevel());
                    enumCategoryDto.setNum(enumCategoryService.getCountByFarm(enumId));
                }
            } else if (classification == 1) {
                for (EnumCategoryDto enumCategoryDto : enumCategories) {
                    String enumId = SubStringCategoryCodeUtil.parseCategoryCode(enumCategoryDto.getId(), enumCategoryDto.getCategoryLevel());
                    enumCategoryDto.setNum(enumCategoryService.getCountByProduct(enumId));
                }
            }

        } catch (Exception ex) {
            //...
        }
        return new HttpResponseEntity<List<EnumCategoryDto>>(enumCategories, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<EnumCategory>> getCategoryByRange(@ApiParam(value = "最小分类id", required = true) @RequestParam(value = "min", required = true) String min, @ApiParam(value = "最大分类id", required = true) @RequestParam(value = "max", required = true) String max) {
        int levelId = 0;
        List<EnumCategory> enumCategories = null;
        try {
            enumCategories = enumCategoryService.getCategoryByRange(min, max);
        } catch (Exception ex) {
            //...
        }
        return new HttpResponseEntity<List<EnumCategory>>(enumCategories, HttpStatus.OK);
    }
}
